package com.df.bpm.utils.entityCreator;

import java.io.File;
import java.io.FileWriter;
import java.nio.file.Paths;

public class EntityFittingCreator {

    public static String modelName="businessOverview";
    public static void main(String[] args) {

        String entityName = "BusinessInformationDTO4Search";//TODO 目标实体类名称

        String sourceRootPath = modelName+"/src/main/java";//TODO 本项目父级路径，通常固定

        String department="";//TODO 各类配套文件路径下子级部门路径名

        try {
            new EntityFittingCreator().createFittings(sourceRootPath,entityName,department);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 创建实体类配套文件
     *
     * @param sourceRootPath 项目父级路径，通常是src/main/java
     * @param entityName     实体类名称
     * @param department    部门路径名
     */
    private void createFittings(String sourceRootPath, String entityName, String department) throws Exception {
        createMapper(sourceRootPath,entityName,department);
        createService(sourceRootPath,entityName,department);
        createServiceImpl(sourceRootPath,entityName,department);
    }

    /**
     * 创建serviceImpl文件
     *
     * @param sourceRootPath 项目父级路径，通常是src/main/java
     * @param entityName     实体类名称
     * @param department    部门路径名
     */
    private void createServiceImpl(String sourceRootPath, String entityName, String department) throws Exception {
        // 获取包名
        String packageName = EntityFittingCreator.class.getPackage().getName();

        // 替换包名中的点号为文件路径分隔符
        String packagePath = packageName.replace('.', File.separatorChar);

        // 构建上级目录路径
        String parentPath = new File(packagePath).getParent();
        parentPath = new File(parentPath).getParent();//再上一级路径

        // 拼接entity包的路径
        String entityPath = parentPath + File.separator + "service"+ File.separator +"impl";
        if(!department.isEmpty()){
            entityPath=entityPath+File.separator
                    +Character.toLowerCase(department.charAt(0)) + department.substring(1);
        }

        String newClassName= Character.toUpperCase(entityName.charAt(0)) + entityName.substring(1);
        String filePath = Paths.get(sourceRootPath, entityPath, newClassName+"ServiceImpl.java").toString();

        // 创建File对象
        File file = new File(filePath);

        // 检查entity目录是否存在
        if (file.getParentFile().exists() || file.getParentFile().mkdirs()) {
            if (file.createNewFile()) {
                System.out.println("文件创建: " + filePath);
                try (FileWriter writer = new FileWriter(file)) {
                    String Data=serviceImplFormat(entityPath.replace(File.separatorChar,'.'),entityName,department);
                    writer.write(Data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("文件已存在: " + filePath);
            }
        } else {
            System.out.println("路径无法创建: " + entityPath);
        }
    }

    /**
     * 数据内容整理serviceImpl
     *
     * @param entityPath 包路径
     * @param entityName 实体类名称
     * @param department 部门路径名
     * @return 整理好的实体类数据内容
     */
    public String serviceImplFormat(String entityPath, String entityName, String department){
        if(!department.isEmpty()){
            department=department+".";
        }
        String elementName=Character.toLowerCase(entityName.charAt(0)) + entityName.substring(1);
        entityName=Character.toUpperCase(entityName.charAt(0)) + entityName.substring(1);
        String newClassName=entityName +"ServiceImpl";
        StringBuilder data= new StringBuilder("package " + entityPath + "; \n\n");
        data.append("import com.coolled.common.service.impl.BaseServiceImpl;\n")
                .append("import com.coolled."+modelName+".entity.").append(department).append(entityName).append(";\n")
                .append("import com.coolled."+modelName+".mapper.").append(department).append(entityName).append("Mapper;\n")
                .append("import com.coolled."+modelName+".service.").append(department).append(entityName).append("Service;\n\n")
                .append("import lombok.RequiredArgsConstructor;\n")
                .append("import org.springframework.stereotype.Service;\n\n")
                .append("@Service\n")
                .append("@RequiredArgsConstructor\n");
        data.append("public class ").append(newClassName)
                .append(" extends BaseServiceImpl<")
                .append(entityName).append("Mapper,").append(entityName)
                .append("> implements ").append(entityName).append("Service {\n");
        data.append("\tpublic final ").append(entityName).append("Mapper ").append(elementName).append("Mapper;\n");
        data.append("}");
        return data.toString();
    }

    /**
     * 创建service文件
     *
     * @param sourceRootPath 项目父级路径，通常是src/main/java
     * @param entityName     实体类名称
     * @param department    部门路径名
     */
    private void createService(String sourceRootPath, String entityName, String department) throws Exception {
        // 获取包名
        String packageName = EntityFittingCreator.class.getPackage().getName();

        // 替换包名中的点号为文件路径分隔符
        String packagePath = packageName.replace('.', File.separatorChar);

        // 构建上级目录路径
        String parentPath = new File(packagePath).getParent();
        parentPath = new File(parentPath).getParent();//再上一级路径

        // 拼接entity包的路径
        String entityPath = parentPath + File.separator + "service";
        if(!department.isEmpty()){
            entityPath=entityPath+File.separator
                    +Character.toLowerCase(department.charAt(0)) + department.substring(1);
        }

        String newClassName= Character.toUpperCase(entityName.charAt(0)) + entityName.substring(1);
        String filePath = Paths.get(sourceRootPath, entityPath, newClassName+"Service.java").toString();

        // 创建File对象
        File file = new File(filePath);

        // 检查entity目录是否存在
        if (file.getParentFile().exists() || file.getParentFile().mkdirs()) {
            if (file.createNewFile()) {
                System.out.println("文件创建: " + filePath);
                try (FileWriter writer = new FileWriter(file)) {
                    String Data=serviceFormat(entityPath.replace(File.separatorChar,'.'),entityName,department);
                    writer.write(Data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("文件已存在: " + filePath);
            }
        } else {
            System.out.println("路径无法创建: " + entityPath);
        }
    }

    /**
     * 数据内容整理service
     *
     * @param entityPath 包路径
     * @param entityName 实体类名称
     * @param department 部门路径名
     * @return 整理好的实体类数据内容
     */
    public String serviceFormat(String entityPath, String entityName, String department){
        if(!department.isEmpty()){
            department=department+".";
        }
        entityName=Character.toUpperCase(entityName.charAt(0)) + entityName.substring(1);
        String newClassName=entityName +"Service";
        StringBuilder data= new StringBuilder("package " + entityPath + "; \n\n");
        data.append("import com.coolled.common.service.BaseService;\n")
                .append("import com.coolled."+modelName+".entity.")
                .append(department).append(entityName).append(";\n\n");

        data.append("public interface ").append(newClassName)
                .append(" extends BaseService<").append(entityName).append("> {");

        data.append("\n}");
        return data.toString();
    }
    /**
     * 创建mapper文件
     *
     * @param sourceRootPath 项目父级路径，通常是src/main/java
     * @param entityName     实体类名称
     * @param department    部门路径名
     */
    private void createMapper(String sourceRootPath, String entityName, String department) throws Exception {
        // 获取包名
        String packageName = EntityFittingCreator.class.getPackage().getName();

        // 替换包名中的点号为文件路径分隔符
        String packagePath = packageName.replace('.', File.separatorChar);

        // 构建上级目录路径
        String parentPath = new File(packagePath).getParent();
        parentPath = new File(parentPath).getParent();//再上一级路径

        // 拼接entity包的路径
        String entityPath = parentPath + File.separator + "mapper";
        if(!department.isEmpty()){
            entityPath=entityPath+File.separator
                    +Character.toLowerCase(department.charAt(0)) + department.substring(1);
        }

        String newClassName= Character.toUpperCase(entityName.charAt(0)) + entityName.substring(1);
        String filePath = Paths.get(sourceRootPath, entityPath, newClassName+"Mapper.java").toString();

        // 创建File对象
        File file = new File(filePath);

        // 检查entity目录是否存在
        if (file.getParentFile().exists() || file.getParentFile().mkdirs()) {
            if (file.createNewFile()) {
                System.out.println("文件创建: " + filePath);
                try (FileWriter writer = new FileWriter(file)) {
                    String Data=mapperFormat(entityPath.replace(File.separatorChar,'.'),entityName,department);
                    writer.write(Data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("文件已存在: " + filePath);
            }
        } else {
            System.out.println("路径无法创建: " + entityPath);
        }
    }

    /**
     * 数据内容整理mapper
     *
     * @param entityPath 包路径
     * @param entityName 实体类名称
     * @param department 部门路径名
     * @return 整理好的实体类数据内容
     */
    public String mapperFormat(String entityPath, String entityName, String department){
        if(!department.isEmpty()){
            department=department+".";
        }
        entityName=Character.toUpperCase(entityName.charAt(0)) + entityName.substring(1);
        String newClassName=entityName +"Mapper";
        StringBuilder data= new StringBuilder("package " + entityPath + "; \n\n");
        data.append("import com.baomidou.mybatisplus.core.mapper.BaseMapper;\n")
                .append("import com.coolled."+modelName+".entity.")
                .append(department).append(entityName).append(";\n\n");

        data.append("public interface ").append(newClassName).
                append(" extends BaseMapper<").append(entityName).append("> {");

        data.append("\n}");
        return data.toString();
    }
}


